home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSKCNT.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  3KB  |  171 lines

  1. /*
  2.    TSKCNT.C - CTask - Counter handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16. /*
  17.    create_counter - initialises counter.
  18. */
  19.  
  20. counterptr far create_counter (counterptr cnt
  21. #if (TSK_NAMEPAR)
  22.                               ,byteptr name
  23. #endif
  24.                               )
  25. {
  26. #if (TSK_DYNAMIC)
  27.    if (cnt == NULL)
  28.       {
  29.       if ((cnt = tsk_alloc (sizeof (counter))) == NULL)
  30.          return NULL;
  31.       cnt->flags = F_TEMP;
  32.       }
  33.    else
  34.       cnt->flags = 0;
  35. #endif
  36.  
  37.    cnt->wait_set = cnt->wait_clear = NULL;
  38.    cnt->state = 0;
  39.  
  40. #if (TSK_NAMED)
  41.    tsk_add_name (&cnt->name, name, TYP_COUNTER, cnt);
  42. #endif
  43.  
  44.    return cnt;
  45. }
  46.  
  47.  
  48. /*
  49.    delete_counter - kills all processes waiting for counter
  50. */
  51.  
  52. void far delete_counter (counterptr cnt)
  53. {
  54.    CRITICAL;
  55.  
  56.    C_ENTER;
  57.    tsk_kill_queue (&(cnt->wait_set));
  58.    tsk_kill_queue (&(cnt->wait_clear));
  59.    cnt->state = 0L;
  60.    C_LEAVE;
  61.  
  62. #if (TSK_NAMED)
  63.    tsk_del_name (&cnt->name);
  64. #endif
  65.  
  66. #if (TSK_DYNAMIC)
  67.    if (cnt->flags & F_TEMP)
  68.       tsk_free (cnt);
  69. #endif
  70. }
  71.  
  72.  
  73. /*
  74.    clear_counter  - Sets counter to zero. All tasks waiting for
  75.                     Counter zero are made eligible.
  76. */
  77.  
  78. void far clear_counter (counterptr cnt)
  79. {
  80.    CRITICAL;
  81.  
  82.    C_ENTER;
  83.    cnt->state = 0L;
  84.    while (cnt->wait_clear != NULL)
  85.       cnt->wait_clear = tsk_runable (cnt->wait_clear);
  86.    C_LEAVE;
  87. }
  88.  
  89. /*
  90.    wait_counter_set  - Wait until counter is != 0. If counter is != 0 on
  91.                        entry, the counter is decremented and the task
  92.                        continues to run. If the counter is decremented to 
  93.                        zero, tasks waiting for zero are made eligible.
  94. */
  95.  
  96. int far wait_counter_set (counterptr cnt, dword timeout)
  97. {
  98.    CRITICAL;
  99.  
  100.    C_ENTER;
  101.    if (cnt->state)
  102.       {
  103.       if (!--cnt->state)
  104.          while (cnt->wait_clear != NULL)
  105.             cnt->wait_clear = tsk_runable (cnt->wait_clear);
  106.       C_LEAVE;
  107.       return 0;
  108.       }
  109.  
  110.    tsk_current->retptr = NULL;
  111.    tsk_wait (&cnt->wait_set, timeout);
  112.    return (int)tsk_current->retptr;
  113. }
  114.  
  115. /*
  116.    wait_counter_clear - Wait until counter is == 0. If counter is == 0 on
  117.                        entry, the task continues to run.
  118. */
  119.  
  120. int far wait_counter_clear (counterptr cnt, dword timeout)
  121. {
  122.    CRITICAL;
  123.  
  124.    C_ENTER;
  125.    if (!cnt->state)
  126.       {
  127.       C_LEAVE;
  128.       return 0;
  129.       }
  130.  
  131.    tsk_current->retptr = NULL;
  132.    tsk_wait (&cnt->wait_clear, timeout);
  133.    return (int)tsk_current->retptr;
  134. }
  135.  
  136.  
  137. /*
  138.    inc_counter - Increment counter. If there are tasks waiting for the
  139.                  set state, the first task in the queue is made eligible.
  140. */
  141.  
  142. void far inc_counter (counterptr cnt)
  143. {
  144.    tcbptr curr;
  145.  
  146.    CRITICAL;
  147.  
  148.    C_ENTER;
  149.    if ((curr = cnt->wait_set) == NULL)
  150.       {
  151.       cnt->state++;
  152.       C_LEAVE;
  153.       return;
  154.       }
  155.    cnt->wait_set = tsk_runable (curr);
  156.    C_LEAVE;
  157. }
  158.  
  159.  
  160. /*
  161.    check_counter - return current counter state.
  162. */
  163.  
  164. dword far check_counter (counterptr cnt)
  165. {
  166.    return cnt->state;
  167. }
  168.  
  169.  
  170.  
  171.